<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@include file="/include/taglibs.jsp" %>
<head>
    <%@include file="/include/head-module.jsp" %>
    <title>新用户注册专享</title>
    <script src="${ctx}/libjs/plugins/tripledes/tripledes.js"></script>
    <script src="${ctx}/libjs/plugins/mode-ecb/mode-ecb.js"></script>
    <link rel="stylesheet" href="${ctx}/static/css/invite.css">
    <style>
        input{
            -webkit-appearance: none;
            outline: none;
        }
    </style>
</head>
<body class="inviteBody">
<div class="topSection">
    <div class="userMsg">
        <img src="${memberInfo.headImg}" class="headImg" alt="">
        <div class="nickName">${memberInfo.nickname}</div>
        <img src="${ctx}/static/img/invite/coin.png" class="coin1" alt="">
        <img src="${ctx}/static/img/invite/coin.png" class="coin2" alt="">
    </div>
    <div class="tips">${marketActivityAndTicketVo.remark}</div>
</div>
<div class="wrapper-content" style="height:auto">
<div class="centerSection">
    <h1>手机号注册</h1>
    <input type="tel" class="phone" maxlength="11" name="mobile" placeholder="请输入手机号码">
    <span class="mobileErrorTips errorTips hide">此手机号已注册过账户，换个手机号吧！</span>
    <input type="tel" class="authCode" maxlength="6" name="authCode" oninput="if(value.length>6) value = value.slice(0,6)" placeholder="请输入验证码"><button type="button" class="getCodeBtn" id="identifyingCode" onclick="getAuthCode()" style="vertical-align: middle!important;">获取验证码</button>
    <span class="authCodeErrorTips errorTips hide">验证码有误，请重新输入！</span>
    <button type="button" class="registerBtn" onclick="doLogin()">马上注册，立享优惠</button>
</div>
<div class="layer-msg">
    <div class="layer-msg-ctx"></div>
</div>
</div>
</body>
<script>
    document.body.addEventListener('focusout', () => {
        //软键盘收起的事件处理
        setTimeout(() => {
            const scrollHeight =
                document.documentElement.scrollTop || document.body.scrollTop || 0;
            window.scrollTo(0, Math.max(scrollHeight - 1, 0));
        }, 100);
    });
</script>
<script>
    console.log("")
    var ajaxReady=true;
    var wait = 60;
    //DES 解密 加密
    function encryptByDES(message, key) {
        var keyHex = CryptoJS.enc.Utf8.parse(key);
        var encrypted = CryptoJS.DES.encrypt(message, keyHex, {
            mode: CryptoJS.mode.ECB,
            padding: CryptoJS.pad.Pkcs7
        });
        return encrypted.toString();
    }
    //获取验证码
    function getAuthCode() {
        if(ajaxReady){
            var mobile=$(' input[name="mobile"]').val();
            if (!/^1[3-9]\d{9}$/.test(mobile)){
                layer.msg("手机号码不符合格式");
                return
            }
            $.ajax({
                url:"${ctx}/login/selectMemberByRegisterMobile",
                type:"post",
                data:{"mobile":mobile},
                beforeSend:function () {
                    ajaxReady=false
                },
                success:function (data) {
                    if (data.data){
                        $(".mobileErrorTips").removeClass("hide");
                        $(".authCode").val("");
                    }else{
                        <%--location.href="${ctx}/front/page/registerActivities/registerSuccess.jsp";--%>
                        $(".mobileErrorTips").addClass("hide");
                        $.ajax({
                            <%--url:"${ctx}/login/genAuthCodeForLogin",--%>
                            url:"${ctx}/invite/getAuthCode",
                            type:"post",
                            data:{"mobile":mobile},
                            beforeSend:function () {
                                ajaxReady=false
                            },
                            success:function (data) {
                                layer.msg(data.message);
                                if(data.status==200){
                                    time();
                                    function time() {
                                        var icon=$('#identifyingCode');
                                        if (wait === 0) {
                                            icon.css('background', 'linear-gradient(180deg,#fe5757, #ff0624)');
                                            icon.html('获取验证码');
                                            icon.attr('onclick','getAuthCode()');
                                            wait = 60;
                                        } else {
                                            icon.css('background', '#bdc3c8');
                                            icon.html(wait + "秒后重试");
                                            icon.removeAttr('onclick');
                                            wait--;
                                            setTimeout(function () {
                                                time();
                                            }, 1000);
                                        }
                                    }
                                }
                            },
                            error:function () {
                                layer.msg("发送失败")
                            },
                            complete:function () {
                                ajaxReady=true
                            }
                        })
                    }
                },
                error:function () {
                    layer.msg("发送失败")
                },
                complete:function () {
                    ajaxReady=true
                }
            })
        }
    }
    //执行登陆
    function doLogin() {
        if(ajaxReady){
            var mobile=$(' input[name="mobile"]').val();
            if (!/^1[3-9]\d{9}$/.test(mobile)){
                layer.msg("手机号码不符合格式");
                return
            }
            var authCode=$(' input[name="authCode"]').val();
            if (authCode==""){
                layer.msg("请填写验证码");
                return
            }
            if(!$(".mobileErrorTips").hasClass("hide")){
                return
            }
            $.ajax({
                url:"${ctx}/invite/loginByAuthCode",
                type:"post",
                data:{"mobile":mobile,"authCode":authCode,"inviteMemberId":${memberInfo.memberId}},
                beforeSend:function () {
                    ajaxReady=false
                },
                success:function (data) {
                    console.log(data)
                    if(data.status==200){
                        location.href="${ctx}/templates/invite/registerSuccess.jsp?memberId=${memberInfo.memberId}&tenantCode=${memberInfo.tenantCode}&wechatOriginalid=${memberInfo.wechatOriginalid}";
                    }else if(data.message=="验证码错误"){
                        $(".authCodeErrorTips").removeClass("hide");
                    }else{
                        layer.msg(data.message);
                    }
                },
                error:function () {
                    layer.msg("发送失败")
                },
                complete:function () {
                    ajaxReady=true
                }
            })
        }
    }
</script>

<script src="${ctx}/appjs/garvey.js"></script>
</html>